package com.reptile.util;


import com.google.common.collect.Lists;
import com.reptile.constant.YsReptileConstant;
import com.reptile.dto.MaterialDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.time.LocalDate;
import java.util.List;


/**
 * 爬取图片核心工具类
 */
@Slf4j
public class PictureInfoDataUtil {

    /**
     * 爬取搜狗壁纸方法
     *
     * @param document
     * @return List<NewsDTO>
     */
    public static List<MaterialDTO> SoGouPicParseList(Document document) {
        //根据网页标签解析源码
        Elements elements = document.select(".high-quality-box");
        List<MaterialDTO> result = Lists.newArrayList();
        Elements elements1 = elements.select("ul").select("li");
        for (Element nextChild : elements1) {
            //获取img 地址
            String url = nextChild.select("img[src]").attr("src");
            if (StringUtils.isBlank(url)) {
                continue;
            }
            MaterialDTO ysMaterialVO = MaterialDTO.builder()
                    .materName(RandomNumberUtil.getRandom(5))
                    .materType(YsReptileConstant.MATER_TYPE_PIC)
                    .materUrl(url)
                    .creatorDate(LocalDate.now())
                    .build();
            result.add(ysMaterialVO);
        }
        return result;
    }


    /**
     * 爬取wallHaven壁纸方法
     *
     * @param document
     * @return List<NewsDTO>
     */
    public static List<MaterialDTO> wallHavenPicParseList(Document document) {
        //根据网页标签解析源码
        Elements elements = document.select("section");
        List<MaterialDTO> result = Lists.newArrayList();
        for (int i = 0; i < elements.size(); i++) {
            Elements sectionItem = elements.select("section").get(i).select("ul").select("li");
            for (Element li : sectionItem) {
                //img url
                String url = li.select("img").attr("data-src");
                MaterialDTO ysMaterialVO = MaterialDTO.builder()
                        .materName(RandomNumberUtil.getRandom(5))
                        .materType(YsReptileConstant.MATER_TYPE_PIC)
                        .materUrl(url)
                        .creatorDate(LocalDate.now())
                        .build();
                result.add(ysMaterialVO);
            }
        }
        return result;
    }
}
